package com.sh169.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.sh169.beans.UserHolder;
import com.sh169.pojos.Order;
import com.sh169.pojos.OrderInfo;
import com.sh169.pojos.ShoppingCart;
import com.sh169.pojos.User;
import com.sh169.util.BaseHttpFilter;
@IocBean(name="shoppingCartController")
@InjectName("shoppingCartController")
public class ShoppingCartController {
	
	//将产品添加到购物车
	@Ok("redirect:/shoppingCart/browse")
	public void addProduct2Cart(@Param("productid")int productid,@Param("num")int num,HttpServletRequest req){
		User user = getCurrentUser(req);
		if(user==null || productid<1 || num<1) return ;
		ShoppingCart sc1 = dao.fetch(ShoppingCart.class,Cnd.where("userid", "=", user.getId()).and("productid", "=", productid));
		if(sc1 != null){
			sc1.setProductnum(sc1.getProductnum()+num);
			dao.update(sc1);
			return ;
		}
		ShoppingCart sc = new ShoppingCart();
		sc.setProductid(productid);
		sc.setUserid(user.getId());
		sc.setProductnum(num);
		
		dao.fastInsert(sc);
	}
	// 浏览购物车内容
	@Ok("tiles:shoppingCart.browse")
	public List<ShoppingCart> browse(HttpServletRequest req){
		User user = getCurrentUser(req);
		List<ShoppingCart> scLists = dao.query(ShoppingCart.class, Cnd.where("userid","=",user.getId()), null);
		for(ShoppingCart sc :scLists){
			dao.fetchLinks(sc, "product");
		}
		return scLists.size()==0?null:scLists;
	}
	// 生成订单
	@Ok("tiles:shoppingCart.createOrder")
	public void createOrder(@Param("::o.")Order order ,HttpServletRequest req){
		List<ShoppingCart> shopCarts = browse(req);
		if(shopCarts ==null) return ;
		User user = getCurrentUser(req);
		order.setCreatetime(new Date());
		order.setUserid(user.getId());
		order.setState(Order.STATE_NEW);
		float price = 0.0f;
		for(ShoppingCart sc : shopCarts){
			price += sc.getProductnum() * sc.getProduct().getPrice();
		}
		order.setPrice(price);
		dao.insert(order);
		for(ShoppingCart sc : shopCarts){
			OrderInfo oi = new OrderInfo();
			oi.setOrderid(order.getId());
			oi.setProductid(sc.getProductid());
			oi.setProductnum(sc.getProductnum());
			dao.fastInsert(oi);
		}
		dao.clear(ShoppingCart.class,Cnd.where("userid","=",user.getId()));
	}
	//会员查看自己订单  需分页
	@Ok("tiles:shoppingCart.viewMyOrders")
	public List<Order> viewMyOrders(HttpServletRequest req){
		User user = getCurrentUser(req);
		List<Order> orders = dao.query(Order.class, Cnd.where("userid","=",user.getId()), null);
		return orders.size()==0?null:orders;
	}
	@Ok("tiles:shoppingCart.showOrder")
	public Order showOrder(@Param("id")int id,HttpServletRequest req){
		if(id<1) return null;
		User user = getCurrentUser(req);
		assert user != null : "用户必须登录";
		Order order = dao.fetch(Order.class, Cnd.where("id","=",id).and("userid","=",user.getId()));
		if(order != null){
			dao.fetchLinks(order, "orderInfos");
			List<OrderInfo> ois = order.getOrderInfos();
			if(ois!= null && ois.size()>0){
				dao.fetchLinks(ois, "product");
			}
		}
		return order;
	}
	
	@Inject("refer:dao")
	private Dao dao;
	
	public Dao getDao() {
		return dao;
	}
	public void setDao(Dao dao) {
		this.dao = dao;
	}
	private User getCurrentUser(HttpServletRequest req){
		UserHolder uh = (UserHolder)req.getSession().getAttribute(BaseHttpFilter.SECURITY_SESSION_USER);
		return  dao.fetch(User.class,Cnd.where("username","=",uh.getUsername()));
	}
}
